package com.forfunnet.minjian.utils;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.graphics.Point;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.IBinder;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.view.inputmethod.InputMethodManager;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Lei on 2014/12/19.
 */
public class CommonUtils {

    public static boolean isValidPhoneNumber(String phoneNo) {
        String expression = "^[0-9-+]{9,15}$";
        CharSequence inputStr = phoneNo;
        Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(inputStr);
        return (matcher.matches())? true : false;
    }

    public static boolean isValidEmail(String target) {
        return android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
    }

    public static String getOsName() {
        return "Android";
    }

    public static void hideSoftInput(Context context, IBinder binder) {
        InputMethodManager manager = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
        manager.hideSoftInputFromWindow(binder, InputMethodManager.HIDE_NOT_ALWAYS);
    }

    public static SimpleDateFormat mFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    public static SimpleDateFormat mDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    public static SimpleDateFormat mMsgDateFormat = new SimpleDateFormat("MM-dd HH:mm");
    public static SimpleDateFormat mMsgYearFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    public static SimpleDateFormat mYearFormat = new SimpleDateFormat("yyyy");

    public static String getTimeString (Date date)
    {
        if (date == null)
        {
            return "未知时间";
        }
        long time = date.getTime();
        long now = System.currentTimeMillis();
        String sp_time = mDateFormat.format(time);
        String current_time = mDateFormat.format(now);
        if (sp_time.equals(current_time))
        {
            long span = now - time;
            long day = span/(24*60*60*1000);
            long hour = (span/(60*60*1000)-day*24);
            long min = ((span/(60*1000))-day*24*60-hour*60);
            long s = (span/1000-day*24*60*60-hour*60*60-min*60);

            if (hour > 0) {
                return String.format("%d小时前", hour);
            }
            else if (min > 10) {
                return String.format("%d分钟前", min);
            }
            else {
                return "刚刚";
            }
        }

        sp_time = mYearFormat.format(time);
        current_time = mYearFormat.format(now);
        if (sp_time.equals(current_time)) {
            return mMsgDateFormat.format(date);
        }
        else {
            return mMsgYearFormat.format(date);
        }
    }

    /**
     * 将 具体时间 转换成 %d时间前
     * @param createTime 输入时间
     * @return %d时间前
     * */

    public static String getTimeString (String createTime)
    {
        try
        {
            Date date = mFormat.parse(createTime);
            return getTimeString(date);
        }
        catch (ParseException e)
        {
            return "未知时间";
        }

    }

    public static String getCompressTimeString(String createTime, boolean showYear)
    {
        try
        {
            Date date = mFormat.parse(createTime);
            Calendar calendar = Calendar.getInstance();
            String format = "MM-dd";

            if (showYear)
            {
                format = "yyyy-" + format;
            }
            if (!createTime.endsWith("00:00:00"))
            {
                format += " HH:mm";
            }
            return new SimpleDateFormat(format).format(date);
        }
        catch (ParseException e)
        {
            return "未知时间";
        }
    }
    public static int getScreenWidth (Activity activity)
    {
        DisplayMetrics displaymetrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        int width = displaymetrics.widthPixels;
        return width;
    }

    public  static Animator translateViewAnimate(View v, float endX, float endY, int duratinMS)
    {
        float x = v.getX();
        float y = v.getY();
        return  translateViewAnimate(v, x, y, endX, endY, duratinMS);
    }
    public  static Animator alphaViewAnimate(View v, float endAlpha, int duratinMS)
    {

        ValueAnimator alpha = ObjectAnimator.ofFloat(v, "alpha", v.getAlpha(), endAlpha);
        alpha.setDuration(duratinMS);
        alpha.setInterpolator(new DecelerateInterpolator());
        alpha.start();
        return  alpha;
    }

    public  static Animator translateViewAnimate(View v, float startX, float startY, float endX, float endY, int duratinMS)
    {
        ValueAnimator xAni = ObjectAnimator.ofFloat(v, "x", startX, endX);
        xAni.setDuration(duratinMS);
        xAni.setInterpolator(new DecelerateInterpolator());

        ValueAnimator yAni = ObjectAnimator.ofFloat(v, "y", startY, endY);
        yAni.setDuration(duratinMS);
        yAni.setInterpolator(new DecelerateInterpolator());

        AnimatorSet aniSet = new AnimatorSet();
        aniSet.playTogether(xAni, yAni);
        aniSet.start();
        return  aniSet;
    }
    public  static String removeMoreNewLineAndTrim(String src)
    {
        String parts[] = src.split("\\n|\\r");
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < parts.length; i++)
        {

            String part = parts[i].trim();
            if (!part.equals(""))
            {
                sb.append(part);
                if (i != parts.length - 1)
                {
                    sb.append("\n");
                }
            }
        }
        return  sb.toString().trim();
    }

    public  static Point screenPointToView(View v, Point ptOnScreen)
    {
        int[] org = new int[2];
        v.getLocationOnScreen(org);
        int offsetX = ptOnScreen.x - org[0];
        int offsetY = ptOnScreen.y - org[1];
        Point ptView = new Point(offsetX, offsetY);
        return  ptView;
    }
}
